<template>
  <div>
    <router-view />
    
    <!-- 全局登录对话框 -->
    <LoginDialog 
      v-model="userStore.showLoginDialog" 
      @loginSuccess="handleGlobalLoginSuccess"
    />
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import LoginDialog from '@/components/LoginDialog.vue'

const userStore = useUserStore()

// 全局登录成功处理
const handleGlobalLoginSuccess = async () => {
  await userStore.getUserInfo()
  ElMessage.success('登录成功')
  userStore.closeLoginDialog()
}

// 应用启动时，如果已登录则获取用户信息
onMounted(async () => {
  if (userStore.isLoggedIn() && !userStore.userInfo) {
    try {
      await userStore.getUserInfo()
      console.log('已自动加载用户信息')
    } catch (error) {
      console.error('自动加载用户信息失败:', error)
      // 如果获取失败，可能是 token 过期，清除登录状态
      userStore.logout()
    }
  }
})
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

#app {
  width: 100%;
  min-height: 100vh;
  margin: 0;
  padding: 0;
}
</style>
